<?php
/*
+ ----------------------------------------------------------------------------------------------+
|     e107 website system  : http://e107.org.ru
|     Released under the terms and conditions of the GNU General Public License (http://gnu.org).
|
|     Plugin "roll_mini"
|     Author: Alex ANP alex-anp@ya.ru
|     Home page: http://e107plugins.blogspot.com
+-----------------------------------------------------------------------------------------------+
*/



require_once("../../class2.php");
if (!getperms("P")) {
      header("location:".e_HTTP."index.php");
      exit;
    }

require_once(e_ADMIN."auth.php");

$lan_file = e_PLUGIN."roll_mini/languages/".e_LANGUAGE.".php";
include_once((file_exists($lan_file) ? $lan_file : e_PLUGIN."roll_mini/languages/English.php"));

$mydb = new db();

// ============= Update settings script ================
if(IsSet($_POST['updatesettings'])) {

    $pref['roll_m_index_size'] = $_POST['roll_m_index_size'];
    $pref['roll_m_cards_in_page'] = $_POST['roll_m_cards_in_page'];

    if ($pref['roll_m_name'] != $_POST['roll_m_name']) {
        $mydb->db_Update("roll_mini_cat", "name = '".$_POST['roll_m_name']."' WHERE id = '1'");
        $pref['roll_m_name'] = $_POST['roll_m_name'];
        }

    $pref['roll_m_fullindex_show'] =  $_POST['roll_m_fullindex_show'];
    $pref['roll_m_search_show'] =  $_POST['roll_m_search_show'];
    $pref['roll_m_comment_show'] = $_POST['roll_m_comment_show'];

    save_prefs();

    $message = "".ROLL_M_L016."";
    $ns->tablerender("", "<div style='text-align:center'><b>$message</b></div>");
}

//========== Create index script ================
if(IsSet($_POST['updateindex'])) {

    $pref['roll_m_fullindex_show'] =  $_POST['roll_m_fullindex_show'];
    $index_list =  $_POST['roll_m_index_list'];

    save_prefs();

    $mydb->db_Delete("roll_mini_cards", "name='-roll_mini_system_name-'");
    $a_index_list = array();
    $a_index_list = explode(" ", $index_list);
    $message .= "".ROLL_M_L003." -";
    foreach ($a_index_list as $index_name) {

    	$table = "roll_mini_cards";
    	$values = array(
            "index_name" => $tp->toDB($index_name),
            "name" => '-roll_mini_system_name-'
            );
    	$mydb->db_Insert($table, $values);

        $message .= " ".$index_name." ";
        }

    $message .= "<br>".ROLL_M_L016."";
    $ns->tablerender("", "<div style='text-align:center'><b>$message</b></div>");

}
// =========== Create linkwords script ===================
if(IsSet($_POST['updatelinkwords'])) {
    $a_values = array();
    $mydb->db_Delete("linkwords", "linkword_word LIKE '%-roll_m_add-%'");

    $mydb->db_Select("roll_mini_cards", "*", "name != '-roll_mini_system_name-'");
    while($row = $mydb->db_Fetch()) {
		   $a_values[] = "'', '0', '".$row['name'].", -roll_m_add-', 'http://".$_SERVER["HTTP_HOST"].e_HTTP.$PLUGINS_DIRECTORY."roll_mini/roll.php?card_id=".$row['id']."'";
           }
    foreach ($a_values as $values) {
        $mydb->db_Insert("linkwords", $values);
        $count++;
    }

    $message .= "".$count." - Line Add <br>".ROLL_M_L016."";
    $ns->tablerender("", "<div style='text-align:center'><b>$message</b></div>");

}

// =========== Create category script ===============
if ($mydb->db_Count("roll_mini_cat", "(*)", "WHERE id != '1'")) {
    $pref['roll_m_categoryes'] = TRUE;
    save_prefs();
} else {
    $pref['roll_m_categoryes'] = FALSE;
    save_prefs();
}

if(IsSet($_POST['create_cat'])) {

if ($pref['roll_m_categoryes'] !== TRUE) {

$sqlqv = "CREATE TABLE #roll_mini_cat (
id INT(9) NOT NULL auto_increment ,
name VARCHAR( 250 ) NULL ,
description TEXT NULL ,
PRIMARY KEY  (id)
) ENGINE = MYISAM ";

$mydb->db_Select_gen($sqlqv);

    $table = "roll_mini_cat";
	$values = array(
        "id" => '1',
        "name" => $pref['roll_m_name'],
        "description" => 'Root category'
        );

	$mydb->db_Insert($table, $values);


$sqlqv = "ALTER TABLE #roll_mini_cards ADD id_cat INT(9) NULL";
$mydb->db_Select_gen($sqlqv);

$mydb->db_Update("roll_mini_cards", "id_cat = '1' WHERE name != '-roll_mini_system_name-'");

$message = "Create category list.";

$pref['roll_m_categoryes'] = TRUE;
save_prefs();

}

    $table = "roll_mini_cat";
	$values = array(
        "name" => ROLL_M_L031,
        "description" => 'New ext category'
        );

if ($mydb->db_Insert($table, $values)) $message .= " Create new category.";

    $ns->tablerender("Message", "<div style='text-align:center'><b>$message</b></div>");

}

//=========== Ext cat save script ===============
if(IsSet($_POST['updatecat'])) {

foreach(array_keys($_POST['roll_m_name']) as $id_cat) {

if ($mydb->db_Update("roll_mini_cat", "name='".$tp->toDB($_POST['roll_m_name'][$id_cat])."' WHERE id='".$id_cat."'")) $message .="<br />".$id_cat." - ".$_POST['roll_m_name'][$id_cat]."";

}

    $ns->tablerender("Message", "<div style='text-align:center'><b>$message</b></div>");
}

//=========== Ext cat delete script ===============
if(IsSet($_POST['delete_cat'])) {

foreach(array_keys($_POST['delete_cat']) as $id_cat) {

    if ($mydb->db_Delete("roll_mini_cat", "id = '".$id_cat."'")) $message .= "<br />Catalog ".$id_cat." - deleted.";
    if ($mydb->db_Delete("roll_mini_cards", "id_cat = '".$id_cat."'")) $message .= "<br />Catalog ".$id_cat." cards - deleted.";

}
    $ns->tablerender("Message", "<div style='text-align:center'><b>$message</b></div>");
}

//======= Admin form ==================
$text = "<form name='setings' action='".e_SELF."' method='post'>
<table style='width:85%' class='fborder'>

<tr>
<td class='forumheader3' colspan='2'><b>".ROLL_M_L018."</b></td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L022."</td>
<td class='forumheader3'>
<input class='tbox' type='text' name='roll_m_name' size='60' value='".$pref['roll_m_name']."' >
</td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L017."</td>
<td class='forumheader3'><input class='tbox' type='text' name='roll_m_index_size' size='10' value='".$pref['roll_m_index_size']."' ></td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L019."</td>
<td class='forumheader3'><input class='tbox' type='text' name='roll_m_cards_in_page' size='10' value='".$pref['roll_m_cards_in_page']."' ></td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L024."</td>
<td class='forumheader3'>" . ($pref['roll_m_fullindex_show'] ? "<input type='checkbox' name='roll_m_fullindex_show' value='1' checked='checked' /> " : "<input type='checkbox' name='roll_m_fullindex_show' value='1' /> ")."
".ROLL_M_L025." <br>
<input class='tbox' type='text' name='roll_m_index_list' size='60' value='".ROLL_M_L023."' >
<input class='button' type='submit' name='updateindex' value='".ROLL_M_L010."/".ROLL_M_L011."'>
</td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L026.":</td>
<td class='forumheader3'>
" . ($pref['roll_m_search_show'] ? "<input type='checkbox' name='roll_m_search_show' value='1' checked='checked' /> " : "<input type='checkbox' name='roll_m_search_show' value='1' /> ")."
".ROLL_M_L025."
</td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L029.":</td>
<td class='forumheader3'>
" . ($pref['roll_m_comment_show'] ? "<input type='checkbox' name='roll_m_comment_show' value='1' checked='checked' /> " : "<input type='checkbox' name='roll_m_comment_show' value='1' /> ")."
".ROLL_M_L025."
</td>
</tr>

<tr>
<td class='forumheader3'>".ROLL_M_L027."</td>
<td class='forumheader3'>";
if ($mydb->db_Count("plugin", "(*)", "WHERE plugin_path='linkwords' AND plugin_installflag=1")) {
    $text .= "<input class='button' type='submit' name='updatelinkwords' value='".ROLL_M_L010."/".ROLL_M_L011."'>";
    } else { $text .= "".ROLL_M_L028.""; }
$text .= "</td>
</tr>

<tr>
<td class='forumheader3' colspan='2'><div align='center'><input class='button' type='submit' name='updatesettings' value='".ROLL_M_L013."'></div></td>
</tr>
</form>
</table>";

//========== Ext categoryes form =================
$text .= "<br /><form name='ext_setings' action='".e_SELF."' method='post'>
<table style='width:85%' class='fborder'>
<tr>
  <td class='forumheader3' colspan='3'>
    <b>".ROLL_M_L030."</b>
    <input class='button' type='submit' name='create_cat' value='".ROLL_M_L010."'>
  </td>
</tr>
";

if ($pref['roll_m_categoryes']) {

$mydb->db_Select("roll_mini_cat", "*", "id > 1 order by id");

while($row = $mydb->db_Fetch()) {
   $text .= "
   <tr>
     <td class='forumheader3'>ID: ".$row['id']."</td>
     <td class='forumheader3'>
     <input class='tbox' type='text' name='roll_m_name[".$row['id']."]' size='60' value='".$row['name']."' >
     <br />URL: <a href='".e_PLUGIN."roll_mini/roll.php?cat=".$row['id']."'>{e_PLUGIN}roll_mini/roll.php?cat=".$row['id']."</a>
     </td>
     <td class='forumheader3'><input class='button' type='submit' name='delete_cat[".$row['id']."]' value='".ROLL_M_L012."' onclick=\"return jsconfirm('".ROLL_M_L032.": ".$row['name']." ID: ".$row['id']." ".ROLL_M_L012."?')\"></td>
   </tr>
   ";
   }
$text .= "
<tr>
  <td class='forumheader3' colspan='3'>
    <div align='center'>
      <input class='button' type='submit' name='updatecat' value='".ROLL_M_L013."'>
    </div>
  </td>
</tr>
";
}

$text .= "
</table></form>
";

$captions = ROLL_M_L001;
$ns -> tablerender($captions, $text);

require_once(e_ADMIN."footer.php");

?>